#pragma once

#include "DX11Includes.h"
#include "EngineStuff.h"

class SpriteFont;

class SpriteRenderer
{

public:

    enum FilterMode
    {
        DontSet = 0,
        Linear = 1,
        Point = 2
    };

    static const UINT64 MaxBatchSize = 1000;

    struct  SpriteDrawData
    {
		XMFLOAT4X4  Transform;
        XMFLOAT4 Color; 
        XMFLOAT4 DrawRect;   
    };    

	SpriteRenderer();
	~SpriteRenderer();

	bool Initialize();

    void Begin(FilterMode filterMode = DontSet);

	bool Render(ID3D11ShaderResourceView* texture, 
				const XMMATRIX& transform, 
                const XMFLOAT4& color = XMFLOAT4(1, 1, 1, 1),
                const XMFLOAT4* drawRect = NULL);

    bool RenderBatch(ID3D11ShaderResourceView* texture,
                     const SpriteDrawData* drawData,
                     UINT64 numSprites);

    void RenderText(const SpriteFont& font, 
                    const char* text,
				    const XMMATRIX& transform, 
                    const XMFLOAT4& color = XMFLOAT4(1, 1, 1, 1));

    void End();

protected:
	void RenderText(const SpriteFont& font, 
                    const WCHAR* text,
				    const XMMATRIX& transform, 
                    const XMFLOAT4& color = XMFLOAT4(1, 1, 1, 1));

	HRESULT CompileShaderFromFile( const char* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut );
    D3D11_TEXTURE2D_DESC SetPerBatchData(ID3D11ShaderResourceView* texture);

	ID3D11Device* device;	
	ID3D11VertexShader* vertexShader;
    ID3D11VertexShader* vertexShaderInstanced;
	ID3D11PixelShader* pixelShader;
	ID3D11Buffer* vertexBuffer;
	ID3D11Buffer* indexBuffer;
    ID3D11Buffer* vsPerBatchCB;
    ID3D11Buffer* vsPerInstanceCB;
    ID3D11Buffer* instanceDataBuffer;
	ID3D11InputLayout* inputLayout;
    ID3D11InputLayout* inputLayoutInstanced;
    ID3D11DeviceContext* context;

    ID3D11RasterizerState* rastState;
    ID3D11DepthStencilState* dsState;
    ID3D11BlendState* alphaBlendState;    
    ID3D11SamplerState* linearSamplerState;
    ID3D11SamplerState* pointSamplerState;

    bool initialized;

    SpriteDrawData textDrawData [MaxBatchSize];

    struct SpriteVertex
    {
        XMFLOAT2 Position;
        XMFLOAT2 TexCoord;
    };

    struct VSPerBatchCB
    {            
        XMFLOAT2 TextureSize;
        XMFLOAT2 ViewportSize;        
    };
    
};
